export default {
	toInt: function(val) {
		val = parseInt(val);
		if (isNaN(val)) val = 0;
		return val;
	},
	toFloat: function(val) {
		val = parseFloat(val);
		if (isNaN(val)) val = 0;
		return Math.round(val * 100) / 100;
	},
	rmoney: function(s) {
		s = s || 0;
		var val = typeof s !== 'number' ? parseFloat(s.replace(/[^\d\.-]/g, "")) : s;
		if (isNaN(val)) val = 0;
		return Math.round(val * 100) / 100;
	},
	formatSize: function(size) {
		size = utils.toInt(size);
		if (size < 1024) return size + "B";
		size = size / 1024;
		if (size < 1024) return utils.toFloat(size) + "KB";
		size = size / 1024;
		if (size < 1024) return utils.toFloat(size) + "MB";
	},
	getFileExt: function(name) {
		var i = name.lastIndexOf('\.'),
			ext = "";
		if (i !== -1) {
			ext = name.substring(i + 1).toLocaleLowerCase();
		}
		return ext;
	},
	curDateTime(format) {
		format = format || '';
		var str = '',
			date = new Date(),
			year = date.getFullYear(),
			month = date.getMonth() + 1,
			day = date.getDate(),
			hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours(),
			minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(),
			second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
		month >= 1 && month <= 9 ? (month = '0' + month) : '';
		day >= 0 && day <= 9 ? (day = '0' + day) : '';

		switch (format) {
			case 'yyyy':
				str = year;
				break;
			case 'yyyyMM':
				str = year + '-' + month;
				break;
			case 'yyyyMMdd':
				str = year + '-' + month + '-' + day;
				break;
			case 'yyyyMMddHHmm':
				str = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
				break;
			default:
				str = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
				break;
		}
		return str;
	},
	getCurrentRouter() {
		let routes = getCurrentPages() // 获取当前打开过的页面路由数组
		let curRoute = routes[routes.length - 1].route // 获取当前页面路由，也就是最后一个打开的页面路由
		let pageUrl = '/' + curRoute

		// let optionsArrLength = JSON.stringify(routes[routes.length - 1].options).split(',').length
		let options = '?'
		for (var i in routes[routes.length - 1].options) {
			options += i + '=' + routes[routes.length - 1].options[i] + '&'
		}
		// const pageId = routes[routes.length - 1].options.id
		// const pageUrl = '/' + curRoute + '?id=' + pageId
		return pageUrl + options
	},
	formatMoney(mVal, suf) {
		var num = mVal?.toString();
		if (!num) return "0.00";
		num = num.toString().replace(/\$|\,/g, ''); // 去除$符号
		if (isNaN(num)) num = "0.00";
		var sign = (num == (num = Math.abs(num)));
		num = Math.floor(num * 100 + 0.50000000001);
		var cents = num % 100;
		num = Math.floor(num / 100).toString();
		if (cents < 10) cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) num = num.substring(0, num.length - (
			4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
		return ((suf ? suf : "") + ((sign) ? '' : '-') + '' + num + '.' + cents);
	},
	// 删除临时文件
	deleteFile(targetDir) {
		// let targetDir = "/storage/emulated/0/Android/data/uni.UNI3F59049/apps/__UNI__3F59049/doc/snapshot"
		try {
			const File = plus.android.importClass('java.io.File');
			const file = new File(targetDir);

			if (!file.exists() || !file.isDirectory()) {
				console.warn('目标路径无效');
				return false;
			}

			// 1. 获取目录下所有文件
			const files = file.listFiles();

			// 2. 定义图片扩展名过滤器
			const imageExts = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp'];

			// 3. 遍历删除图片文件
			for (let i = 0; i < files.length; i++) {
				const currentFile = files[i];
				const name = currentFile.getName().toLowerCase();

				if (imageExts.some(ext => name.endsWith(ext))) {
					const result = currentFile.delete();
					console.log(`文件 ${name} 删除 ${result ? '成功' : '失败'}`);
				}
			}

			return true;
		} catch (err) {
			console.error('原生删除失败:', err);
			return false;
		}
	}
}